<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use App\User;
use App\MenuOption;
use App\AdminMenu;
use App\RoleMenu;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

	/**
	 *  判断当前角色可见的操作按钮
	 * 1、获取当前点击的菜单path,查出菜单id
	 * 2、获取当前菜单的按钮的option_id
	 * 3、判断当前用户的角色，获取角色可见菜单的按钮 option_ids
	 * 4、判断 option_id是否在option_ids数组中
	 */
	// 验证菜单可使用的功能权限--------------------------start-----------------------------------------------------
	public function  checkMenuOption($uri){

		if(!$uri || $uri == ''){
			return array();
		}

		$menu_id = AdminMenu::where('uri',$uri)->pluck('id')->toArray()[0];
		// 获取当前菜单的按钮的option_id
		$menuOpIds = MenuOption::getMenuOptionId($menu_id);

		// 判断当前用户的角色，获取角色可见菜单的按钮 option_ids
		$uid = Auth::user()->id;
		$role_id = User::getUserIdByRole($uid);
		$option_idsArr  = RoleMenu::where('menu_id',$menu_id)->where('role_id',$role_id)->select('option_ids')->get()->toArray();
		$option_ids = array();
		if($option_idsArr){
			foreach ($option_idsArr as $value){
				$option_ids = explode(',',$value['option_ids']);
			}
		}

		// 判断 option_id是否在option_ids数组中
		$optionArr = array();
		foreach ($menuOpIds as $key=>$value){
			if(in_array($value->option_id,$option_ids)){
				$optionArr[] = $value->option_id;
			}
		}
		return $optionArr;

	}








}
